import 'package:flutter/material.dart';

import '../../constant/colors.dart';
import '../buttons.dart';
import '../magic_image.dart';

/// 无内容/网络错误占位
class EmptyPlaceholder extends StatelessWidget {
  /// 无内容占位
  const EmptyPlaceholder({
    super.key,
    this.imageName = 'placeholder_empty',
    this.text = '暂无数据~',
    this.textColor = MagicColors.secondarySubtitle,
    this.imageAndTextSpacing = 6,
    this.yOffset = 0,
    this.textMaxWidth = 200,
    this.buttonTitle,
    this.button,
    this.alignment = Alignment.center,
    this.onTapButton,
  });

  /// 网络错误占位
  const EmptyPlaceholder.networkError({
    super.key,
    this.imageName = 'placeholder_network',
    this.text = '网络波动了一下~',
    this.textColor = MagicColors.secondarySubtitle,
    this.imageAndTextSpacing = 6,
    this.yOffset = 0,
    this.textMaxWidth = 200,
    this.buttonTitle = '重新加载',
    this.button,
    this.alignment = Alignment.center,
    this.onTapButton,
  });

  /// 图片名
  final String? imageName;

  /// 文案
  final String text;

  /// 文案颜色
  final Color textColor;

  /// 图片和文案的垂直间距，默认未 6。
  final double imageAndTextSpacing;

  /// 垂直偏移
  final double yOffset;

  /// 文本最大宽度。默认为 200。
  final double textMaxWidth;

  /// 按钮标题。
  ///
  /// [buttonTitle] == null 则不显示按钮。
  final String? buttonTitle;

  /// 按钮。
  ///
  /// 如果 [button] 与 [buttonTitle] 都有值，优先使用 [button]。
  final Widget? button;

  final AlignmentGeometry? alignment;

  /// 点击按钮回调。
  final VoidCallback? onTapButton;

  bool get shouldShowButton => buttonTitle != null || button != null;

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(
        top: yOffset > 0 ? yOffset : 0,
        bottom: yOffset < 0 ? -yOffset : 0,
      ),
      alignment: alignment,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          if (imageName != null)
            Padding(
              padding: EdgeInsets.only(bottom: imageAndTextSpacing),
              child: MagicImage.image(imageName!),
            ),
          textWidget,
          if (shouldShowButton) button ?? buttonWidget,
        ],
      ),
    );
  }

  /// 提示文本
  Widget get textWidget => Container(
        constraints: BoxConstraints(maxWidth: textMaxWidth),
        child: Text(
          text,
          style: TextStyle(fontSize: 14, color: textColor),
          textAlign: TextAlign.center,
        ),
      );

  Widget get buttonWidget => MagicButton.theme(
        margin: const EdgeInsets.only(top: 13),
        padding: const EdgeInsets.symmetric(vertical: 9, horizontal: 22),
        onTap: onTapButton,
        child: Text(
          buttonTitle ?? '',
          style: const TextStyle(fontSize: 14, color: Colors.white),
        ),
      );
}
